#ifndef PREPROCESSOR_H
#define PREPROCESSOR_H

#pragma once
#include <vector>
#include <torch/torch.h>
#include "RinexParser.h"
#include "SP3Interpolator.h"
#include <string>
#include <map>
#include <functional>
#include "Utils.h"
#include "Common.h"
#include "Config.h"

class Preprocessor {
public:
    Preprocessor();
    ~Preprocessor();

    // 设置观测数据
    void setObservations(const std::vector<GNSSObservation>& obs);
    
    // 检测和修复周跳
    void detectAndFixCycleSlips();
    
    // 进行数据质量控制
    void qualityControl(double minElevation = 10.0);
    
    // 获取处理后的观测数据
    std::vector<GNSSObservation> getProcessedObservations() const;

    // 添加新方法
    void setSP3Interpolator(SP3Interpolator* sp3) { sp3Interpolator_ = sp3; }
    void setUsePreciseOrbit(bool use) { usePreciseOrbit_ = use; }

    // 设置周跳检测方法
    void setCycleSlipMethod(CycleSlipMethod method);

    // 设置多路径检测阈值
    void setMultipathThreshold(double threshold);
    
    // 多路径检测与缓解
    void detectAndMitigateMultipath();

    // 权重计算模式
    void setWeightingMode(WeightingMode mode);
    
    // 为观测值计算权重
    void computeObservationWeights();

    // 设置站点信息
    void setStationInfo(const StationInfo& info) { stationInfo_ = info; }

private:
    std::vector<GNSSObservation> observations_;
    std::vector<GNSSObservation> processedObservations_;
    
    SP3Interpolator* sp3Interpolator_ = nullptr;  // SP3插值器指针
    bool usePreciseOrbit_ = false;                // 是否使用精密星历
    
    CycleSlipMethod cycleSlipMethod_ = CycleSlipMethod::GeometryFree;
    
    double multipathThreshold_ = 0.5;  // 多路径检测阈值 (m)
    
    WeightingMode weightingMode_ = WeightingMode::Elevation;
    
    StationInfo stationInfo_;
    
    // 无几何组合检测周跳
    bool detectGeometryFreeCycleSlip(const GNSSObservation& prev, const GNSSObservation& curr, double threshold = 0.1);
    
    // 时间差分检测周跳
    bool detectTimeDifferenceCycleSlip(const GNSSObservation& prev, const GNSSObservation& curr, double threshold = 0.5);
    
    // 电离层变化率检测周跳
    bool detectIonosphereRateCycleSlip(const GNSSObservation& prev, const GNSSObservation& curr, double threshold = 0.05);
    
    // 修复周跳
    void fixCycleSlip(std::vector<GNSSObservation>& satelliteObs, size_t index);

    // 使用精密轨道计算更精确的卫星位置
    void refineSatellitePositions();

    // 计算多路径指数
    double calculateMultipathIndex(const GNSSObservation& obs);
    
    // 使用加权平滑缓解多路径效应
    void mitigateMultipathBySmoothing(std::vector<GNSSObservation>& satelliteObs);

    // 根据高度角计算权重
    double computeElevationWeight(double elevation);
    
    // 根据信噪比计算权重
    double computeSNRWeight(double snr);
    
    // 根据多路径指数计算权重
    double computeMultipathWeight(double mp);
    
    // Melbourne-Wübbena周跳检测
    bool detectMWCycleSlip(const GNSSObservation& prev, const GNSSObservation& curr, double threshold = 0.75);
    
    // TurboEdit (组合)周跳检测
    bool detectTurboEditCycleSlip(const GNSSObservation& prev, const GNSSObservation& curr);
    
    // High-rate data (高速率数据)周跳检测
    void detectHighRateCycleSlips(std::vector<GNSSObservation>& satelliteObs);
    
    // 计算数据质量（用于自适应周跳检测）
    double calculateDataQuality(const GNSSObservation& prev, const GNSSObservation& curr);
    
    // 更新卫星几何信息
    void updateSatelliteGeometry(GNSSObservation& obs, const std::vector<double>& satPos);
    
    // Group observations by satellite
    std::map<int, std::vector<GNSSObservation>> groupObservationsBySatellite();
    
    // Group observations by epoch
    std::map<std::string, std::vector<GNSSObservation>> groupObservationsByEpoch();
    
    // Update observations from modified groups
    void updateObservationsFromGroups(const std::map<int, std::vector<GNSSObservation>>& groups);
};

#endif // PREPROCESSOR_H